package g11.actionutil;

import g11.dao.AppointmentDAO;
import g11.dao.AppointmentDAOImpl;
import g11.dao.DAOException;
import g11.dao.WorkForDAO;
import g11.dao.WorkForDAOImpl;
import g11.model.Appointment;
import g11.model.Employee;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;

/**
 * @author Fern
 * 
 */

public class AppointmentUtilImpl implements AppointmentUtil {

	/*
	 * given a doctor and a new appointment, check is this new appointment
	 * conflicts with other appointments of this doctor
	 */
	public final boolean checkAppointmentConflicByDoctor(final Employee doctor,
			final Appointment appointment, boolean update) throws DAOException {
		boolean confict = false;
		/* get the doctor's appointment first */
		List<Appointment> appointmentList = new ArrayList<Appointment>();
		AppointmentDAO appDao = new AppointmentDAOImpl();
		appointmentList = appDao.getPendingAppointmentByDoctor(doctor);
		/* if the doctor doesn't have any appointment yet */
		if (appointmentList.isEmpty())
			return false;
		/* get the date and time from this appointment */
		int length = appointmentList.size();
		Date currDate = appointment.getAppointmentDate();
		String currStartTime = appointment.getStartTime();
		String currEndTime = appointment.getEndTime();
		int currId=0;
		if(update){
			/* appointment is being updated; get the current id */
			currId=appointment.getAppointmentId();	
		}
		/* compare the date and time for each appointment */
		for (int i = 0; i < length; i++) {
			/*get the comp Id*/
			int compId=appointmentList.get(i).getAppointmentId();
			/*if the Id's are same, then is updating the same appointment, ignore*/
			if(update &&  (currId==compId)) break;
		
			/* first compare date */
			Date compDate = appointmentList.get(i).getAppointmentDate();
			byte status = appointmentList.get(i).getStatus();

			if (status == 0 && !(compDate.after(currDate))
					&& !(compDate.before(currDate))) {
				/*
				 * if this appointment is pending and on the same day. then need
				 * compare the endtime and starttime
				 */
				String compStartTime = appointmentList.get(i).getStartTime().substring(0, 5);
				String compEndTime = appointmentList.get(i).getEndTime().substring(0, 5);
				/*
				 * if the currStartTime is between compStartTime and
				 * compEndTime, or currEndTime is between compStartTime and
				 * compEndTime then there's a conflict
				 * 
				 * if ((currStartTime.hashCode() < compEndTime.hashCode()) &&
				 * (currStartTime.hashCode() >= compStartTime.hashCode()) ||
				 * ((currEndTime.hashCode() <= compEndTime.hashCode()) &&
				 * (currEndTime .hashCode() > compStartTime.hashCode()))) {
				 */
				if (((currStartTime.compareTo(compEndTime) < 0)
						&& (currStartTime.compareTo(compStartTime) >= 0))
						|| ((currEndTime.compareTo(compEndTime) <= 0) && (currEndTime
								.compareTo(compStartTime) > 0))) {

					confict = true;
					return confict;
				}

			}

		}

		return confict;
	}

	@Override
	public List<Appointment> getTodayAppointmentByStaff(Employee staff)
			throws DAOException {
		WorkForDAO workForDAO = new WorkForDAOImpl();
		AppointmentDAO appointmentDAO = new AppointmentDAOImpl();
		List<Employee> doctorList = new ArrayList<Employee>();
		List<Appointment> returnAppointmentList = new ArrayList<Appointment>();
		try {
			doctorList = workForDAO.getDoctorList(staff);
			for (Employee doctor : doctorList) {
				List<Appointment> tempAppointmentList = new ArrayList<Appointment>();
				tempAppointmentList = appointmentDAO
						.getTodayAppointmentByDoctor(doctor);
				for (Appointment tempAppointment : tempAppointmentList) {
					returnAppointmentList.add(tempAppointment);
				}
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		}
		return returnAppointmentList;
	}

	@Override
	public List<Appointment> getAppointmentByStaff(Employee staff)
			throws DAOException {
		WorkForDAO workForDAO = new WorkForDAOImpl();
		AppointmentDAO appointmentDAO = new AppointmentDAOImpl();
		List<Employee> doctorList = new ArrayList<Employee>();
		List<Appointment> returnAppointmentList = new ArrayList<Appointment>();
		try {
			doctorList = workForDAO.getDoctorList(staff);
			for (Employee doctor : doctorList) {
				List<Appointment> tempAppointmentList = new ArrayList<Appointment>();
				tempAppointmentList = appointmentDAO
						.getPendingAppointmentByDoctor(doctor);
				for (Appointment tempAppointment : tempAppointmentList) {
					returnAppointmentList.add(tempAppointment);
				}
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		}
		return returnAppointmentList;

	}

}
